package atm;

import java.util.Scanner;

public class Atm {
    static String[][] users ={
            {"aaaaaa","111111","1000"},//账号 ，密码， 余额
            {"bbbbbb","222222","2000"},
            {"cccccc","333333","3000"},
            {"dddddd","444444","4000"},
            {"eeeeee","555555","5000"},
    };
    static Scanner sc=new Scanner(System.in);
    static  int index =0;
    //控制ATM流程的方法
    public static void process(){    //process 过程  涩
        System.out.println("欢迎使用lovo银行");
        while (true){
            longin();
            menu();

        }

    }

    // 登入方法
    public static void longin(){
        for(int i=0;i<=3;i++){
            System.out.println("请输入账号");
            String account=sc.next();
            System.out.println("请输入密码");
            String pwd =sc.next();
            for (int j=0;j<users.length;++j){
                if(users[j][0].equals(account) && users[j][1].equals(pwd)){
                    System.out.println("登入成功");
                    index = j;
                    return;
                }
            }
            System.out.println("账号密码错误，请重新输入,还剩"+(2-i)+"次");
        }
        System.out.println("次数用完，卡已被吞");
        System.exit(0);//系统结束
    }
    //菜单界面
    public static void menu(){
        while(true){
            System.out.println("请输入；1-查询 2-存款 3-取款 4-转账 5-切换账号 6-退出 7-修改密码");
            int input = sc.nextInt();
            if(input == 1){
                search();           //1
            }else if(input == 2){
                save();
            }else if(input == 3){
                get();
            }else if(input == 4){
                transfer();
            }else if (input ==5){
                System.out.println("登入退出");
                break;
            }else if (input ==6){
                System.out.println("谢谢使用，再见");
                System.exit(0);
            }else if (input ==7){
                if (modify()){
                    System.out.println("已登出");
                    return;   //return  返回值
                }

            }else
                System.out.println("选择有误，请重新选择");


        }
    }

    //存款
    public static void save(){
        System.out.println("请输入存入的金额");
        double money =sc.nextDouble();
        users[index][2] = Double.parseDouble(users[index][2])+money+"";
        System.out.println("存款成功,你的余额为"+users[index][2]);
    }

    //取款
    public static void get(){
        System.out.println("请输入取款金额");
        double money =sc.nextDouble();
        if (money<Double.parseDouble(users[index][2])){
            users[index][2] =Double.parseDouble(users[index][2])-money+"";
            System.out.println("取款成功,你的余额为；"+users[index][2]);
        }else {
            System.out.println("你的余额不足");
        }

    }

    //查询
    public static void search(){
        System.out.println("你的余额为；"+users[index][2]);
    }

    //转账
    public static void transfer(){
        System.out.println("请输入转账金额");
        double money =sc.nextDouble();
        //判断余额是否足够
        if(money>Double.parseDouble(users[index][2])){
            System.out.println("余额不足");
            return;
        }
        System.out.println("请输入对方账号；");
        String otherAccount =sc.next();
        //不能转给自己
        if(otherAccount.equals(users[index][0])){
            System.out.println("不能转给自己");
            return;
        }
        //循环判断输入账号是否存在
        for (int i=0;i<users.length;i++){
            if (users[i][0].equals(otherAccount)){
                //当前账户的钱扣去
                users[index][2]=Double.parseDouble(users[index][2])-money+"";
                //把钱加到对方账户上
                users[i][2]=Double.parseDouble(users[i][2])+money+"";
                System.out.println("转账成功");
                System.out.println("你的余额为"+(users[index][2]));
                return;
            }
        }
    }
    //修改密码
    public static boolean modify() {
        System.out.println("请输入老密码");
        String pwd=sc.next();
        if (!pwd.equals(users[index][1])){
            System.out.println("原密码输入错误");
            return false;
        }
        System.out.println("请输入新密码");
        String pwd1 =sc.next();
        if (pwd.equals(pwd1)){
            System.out.println("新密码不能和老密码相同");
            return false;
        }
        System.out.println("请重新输入新密码");
        String pwd2 =sc.next();
        if (!pwd1.equals(pwd2)){
            System.out.println("两次密码不一致");
            return false;//     假
        }
            users[index][1]=pwd2;
        System.out.println("密码修改成功");
        return true;
    }

    public static void main(String[]args){
        process();

    }


}
